//
//  LDTRedioPlayController.m
//  EPA
//
//  Created by lanou3g on 16/4/21.
//  Copyright © 2016年 浮夸. All rights reserved.
//

#import "LDTRedioPlayController.h"
#import "LDTRadioDatailsModel.h"
#import "LDTAVManager.h"
@interface LDTRedioPlayController ()


///标题
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
///封面
@property (weak, nonatomic) IBOutlet UIImageView *coverImageView;
///当前时间
@property (weak, nonatomic) IBOutlet UILabel *currentLabel;
///播放进度
@property (weak, nonatomic) IBOutlet UISlider *rateSlider;
///总时间
@property (weak, nonatomic) IBOutlet UILabel *totalLabel;
///播放按钮
@property (weak, nonatomic) IBOutlet UIButton *playButton;




///计时器
@property(nonatomic,strong)NSTimer *playTimer;

@end

@implementation LDTRedioPlayController

- (void)viewDidLoad {
    [super viewDidLoad];
   self.title = @"电台播放器";
    //将导航栏设置为透明的,label就会出现了
   // self.navigationController.navigationBar.translucent = NO;
    
    //创建背景View
    UIImageView *backView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    backView.image = [UIImage imageNamed:@"毛玻璃"];
    [self.view addSubview: backView];
    [self.view sendSubviewToBack:backView];
    //毛玻璃
    UIBlurEffect *abEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc]initWithEffect:abEffect];
    effectView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [backView addSubview:effectView];
    

    
    
    
    
    LDTRadioDatailsModel *model = self.radioArray[self.currentIndex];
    self.titleLabel.text = model.title;
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    
    
    [LDTAVManager shareManager].currentIndex = self.currentIndex;
    
    NSMutableArray *itemArray = [NSMutableArray new];
    for (LDTRadioDatailsModel *model in self.radioArray) {
        
        NSURL *musicUrl = [NSURL URLWithString:model.musicUrl];
        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:musicUrl];
        [itemArray addObject:item];
    }
    
    //AVPlayerItem数组
    [LDTAVManager shareManager].itemArray = itemArray;
    
    [[LDTAVManager shareManager] play];
    
    //创建Timer
    self.playTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    
}

- (void)timerAction
{
    float current = [LDTAVManager shareManager].player.currentTime.value / [LDTAVManager shareManager].player.currentTime.timescale;
    NSString *currentStr = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)current/60,(NSInteger)current %60];
    self.currentLabel.text = currentStr;
    
    
    float total = 0;
    if ([LDTAVManager shareManager].player.currentItem.duration.timescale >0) {
        
        total = [LDTAVManager shareManager].player.currentItem.duration.value /[LDTAVManager shareManager].player.currentItem.duration.timescale;
    }
    NSString *totalStr = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)total/60,(NSInteger)total %60];
    self.totalLabel.text = totalStr;
    
    
    if (total > 0 ) {
        float rate = current / total;
        self.rateSlider.value = rate;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//上一曲
- (IBAction)didClickPreviousButton:(id)sender
{
    [[LDTAVManager shareManager] previous];
    //图片和标题
    self.currentIndex --;
    if (self.currentIndex <0) {
        self.currentIndex = self.radioArray.count - 1;
    }
    LDTRadioDatailsModel *model = self.radioArray[self.currentIndex];
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.titleLabel.text = model.title;
    
    [self.playButton setImage:[UIImage imageNamed:@"暂停"] forState:(UIControlStateNormal)];
    
}
//播放,暂停
- (IBAction)didClickPlayButton:(id)sender
{
    if ([LDTAVManager shareManager].player.rate == 0) {
        [[LDTAVManager shareManager]play];
        [(UIButton *)sender setImage:[UIImage imageNamed:@"暂停"] forState:(UIControlStateNormal)];
    }else{
        [[LDTAVManager shareManager]pause];
         [(UIButton *)sender setImage:[UIImage imageNamed:@"播放"] forState:(UIControlStateNormal)];
    }
}
//下一曲
- (IBAction)didClickNextButton:(id)sender
{
    [[LDTAVManager shareManager]next];
    self.currentIndex ++;
    if (self.currentIndex >= self.radioArray.count) {
        self.currentIndex = 0;
    }
    
    LDTRadioDatailsModel *model = self.radioArray[self.currentIndex];
    
    
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.titleLabel.text = model.title;
      [self.playButton setImage:[UIImage imageNamed:@"暂停"] forState:(UIControlStateNormal)];
}

//进度条
- (IBAction)didChangeRateAction:(UISlider *)sender
{
    CMTime changeTime = CMTimeMake(sender.value *[LDTAVManager shareManager].player.currentItem.duration.value, [LDTAVManager shareManager].player.currentItem.duration.timescale);
    
    [[LDTAVManager shareManager] seekToTime:changeTime];
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
